package com.dataseek.iot.device.controller;

import com.dataseek.iot.device.entity.AlarmLog;
import com.dataseek.iot.device.service.AlarmLogService;
import com.dataseek.iot.device.vo.AlarmLogVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;


@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/alarm")
public class AlarmController {

    @Autowired
    private AlarmLogService alarmLogService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.GET)
    public ResponseResult queryDeviceList(@RequestParam(value="page")  Integer page,
                                          @RequestParam(value="size")  Integer size,
                                          @RequestParam(value="deviceCode", required=false) String deviceCode,
                                          @RequestParam(value="alarmType", required=false) String alarmType,
                                          @RequestParam(value="jarCode", required=false) String jarCode,
                                          @RequestParam(value="isDeal", required=false) String isDeal,
                                          @RequestParam(value="startTime", required=false) String startTime,
                                          @RequestParam(value="endTime", required=false) String endTime
                                         ) throws IOException {
        AlarmLogVo alarmLogVo = alarmLogService.queryAction(deviceCode, alarmType,jarCode,isDeal,startTime,endTime,page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),alarmLogVo);
    }

    /**
     * 新建告警日志
     *
     * @param alarmLog 告警日志
     * @return
     */
    @RequestMapping(value = "/insertAlarmLog", method = RequestMethod.POST)
    public ResponseResult insertAlarmLog(@RequestBody AlarmLog alarmLog) throws IOException, SQLException {
        Integer code  = alarmLogService.insertAction(alarmLog);
        ResponseResult responseResult;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"新增日志错误!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        return responseResult;
    }
    /*
    更新变更状态
     */
    @RequestMapping(value = "/updateAlarmLog", method = RequestMethod.POST)
    public ResponseResult updateAlarmLog(@RequestBody AlarmLog alarmLog) throws IOException, SQLException {
        Integer code  = alarmLogService.updateAction(alarmLog);
        ResponseResult responseResult;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"新增日志错误!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        return responseResult;
    }

    /*
  批量更新变更状态
   */
    @RequestMapping(value = "/updateAlarmLogStatus", method = RequestMethod.POST)
    public ResponseResult updateAlarmLogStatus(@RequestBody List<Long> alarmLogIds) throws IOException, SQLException {

        AlarmLog alarmLog=new AlarmLog();
        Integer code =0;
        if(alarmLogIds!=null&&alarmLogIds.size()>0) {
            for(Long alarmId:alarmLogIds) {
                code = alarmLogService.updateStatus(alarmId);
            }
        }
        ResponseResult responseResult;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"修改日志错误!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        return responseResult;
    }
}